package com.vhall.component.entity.dictionary.enums;

import com.vhall.component.entity.dictionary.OptionVO;
import com.vhall.component.entity.tag.enums.AdverShelvesEnum;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shimingming
 * @since 2023/4/13 19:46
 */

@Slf4j
@Getter
public enum SelectTypeEnum {
    Select_PlacementLocationEnum("placement_location", "投放位置", PlacementLocationEnum.class),
    Select_AdTypeEnum("ad_type", "投放类型", AdTypeEnum.class),
    Select_ColumnLevelEnum("column_level", "栏目等级", ColumnLevelEnum.class),
    Select_ColumnWarningEnum("column_warning", "栏目运营预警", ColumnWarningEnum.class),
    Select_AdverShelvesEnum("adver_shelves", "广告上下架状态", AdverShelvesEnum.class),

    ;
    public static final Map<String, List<OptionVO>> SelectTypeEnum_MAP = new HashMap<>();

    static {
        for (SelectTypeEnum typeEnum : SelectTypeEnum.values()) {
            Class clazzEnum = typeEnum.getOptionEnum();
            if (!BaseEnum.class.isAssignableFrom(clazzEnum)) {
                log.error("枚举类：" + clazzEnum.getName() + "未实现com.jd.lwls.common.other.BaseEnum！");
            }
            Object[] enumConstants = clazzEnum.getEnumConstants();
            List<OptionVO> optionList = new ArrayList<>();
            try {
                Method getValue = clazzEnum.getMethod("getValue");
                Method getName = clazzEnum.getMethod("getName");
                for (Object obj : enumConstants) {
                    optionList.add(new OptionVO(getValue.invoke(obj), getName.invoke(obj)));
                }
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage(), e);
            }
            SelectTypeEnum_MAP.put(typeEnum.getCode(), optionList);
        }
    }

    SelectTypeEnum(String code, String name, Class<? extends BaseEnum> optionEnum) {
        this.code = code;
        this.name = name;
        this.optionEnum = optionEnum;
    }

    /**
     * 代码
     */
    private String code;
    /**
     * 描述
     */
    private String name;
    /**
     * 完全限制类名
     */
    private Class<? extends BaseEnum> optionEnum;


    public static List<OptionVO> getByCode(String code) {
        return SelectTypeEnum_MAP.get(code);
    }
}

